package com.jcfk.eam.controller.bd;

import cn.hutool.core.util.StrUtil;
import com.jcfk.common.business.enums.AvailableStatus;
import com.jcfk.common.business.log.annotation.RequestLogHeader;
import com.jcfk.common.business.utils.DataBindHelper;
import com.jcfk.common.web.domain.DataPage;
import com.jcfk.common.web.domain.ReqPageParam;
import com.jcfk.common.web.domain.RetPage;
import com.jcfk.common.web.domain.RetResult;

import com.jcfk.eam.domain.dto.base.EamRefAssetPartDTO;
import com.jcfk.eam.domain.dto.base.EamSparePartDTO;
import com.jcfk.eam.domain.query.base.EamSparePartQuery;
import com.jcfk.eam.service.base.EamSparePartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 备件
 *
 * @author awei
 * @email 522891237@gqq.com
 * @date 2024-08-14
 */
@Api(tags = {"备件管理"})
@RestController
@RequestMapping("eamsparepart")
public class EamSparePartController {
    @Autowired
    private EamSparePartService eamSparePartService;

    /**
     * 分页列表
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "ReqPageParam<EamSparePartQuery>", name = "requestParam", value = "", required = true)
    })
    @ApiOperation("分页查询备件")
    @PostMapping("/queryByPage")
    public RetPage<EamSparePartDTO> queryByPage(@RequestBody ReqPageParam<EamSparePartQuery> requestParam) {
        DataPage<EamSparePartDTO> page = new DataPage<EamSparePartDTO>((long) requestParam.getCurrent(),
                (long) requestParam.getSize(),
                requestParam.getSort().getOrderItem(),
                EamSparePartDTO.class);
        page = eamSparePartService.queryByPage(page, requestParam.getData());

        return new RetPage<>(page);
    }


    /**
     * 备件
     * 根据id 查询详情
     */
    @ApiOperation("查询备件")
    @GetMapping("/get/{id}")
    public RetResult<EamSparePartDTO> get(@PathVariable("id") String id) {
        EamSparePartDTO item = eamSparePartService.get(id);
        return RetResult.ok(item);
    }

    /**
     * 添加/更新管理备件
     */
    @ApiOperation("保存备件")
    @PostMapping("/save")
    @RequestLogHeader(modular = "备件管理", businessType = "保存")
    public RetResult<?> save(@RequestBody @Valid EamSparePartDTO eamSparePart) {
        // 数据校验
        RetResult ret = eamSparePartService.isValid(eamSparePart);
        if (!ret.getCode().equals(RetResult.SUCCESS)) {
            return ret;
        }

        if (StrUtil.isEmpty(eamSparePart.getId())) {
            // 新增
            eamSparePartService.insert(eamSparePart);
        } else {
            // 更新
            eamSparePartService.update(eamSparePart);
        }
        return RetResult.ok();

    }

    /**
     * 批量删除备件
     */
    @ApiOperation("批量删除备件")
    @PostMapping("/delete")
    @RequestLogHeader(modular = "备件管理", businessType = "批量删除")
    public RetResult<?> delete(@RequestBody EamSparePartDTO eamSparePart) {
        return RetResult.ok(eamSparePartService.logicDeleteList(eamSparePart));
    }


    /**
     * 删除备件
     */
    @ApiOperation("删除备件")
    @PostMapping("/deleteItem/{id}")
    @RequestLogHeader(modular = "备件管理", businessType = "删除")
    public RetResult<?> deleteItem(@PathVariable("id") String id) {
        eamSparePartService.deleteItem(id);
        return RetResult.ok();
    }

    /**
     * 备件
     * 根据id 查询详情
     */
    @ApiOperation("备件列表")
    @GetMapping("/getList")
    public RetResult<?> getList(@RequestBody EamSparePartQuery query) {
        query.setEnabled(1);
        query.setDeleted(0);
        List<EamSparePartDTO> dtos = eamSparePartService.getList(query);
        return RetResult.ok(dtos);
    }

    /**
     * 获取信息列表
     *
     * @param query
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "EamSparePartQuery", name = "query", value = "", required = true)
    })
    @ApiOperation(value = "获取备件列表", notes = "获取备件列表", httpMethod = "POST")
    @PostMapping("getDetails")
    public RetResult<List<EamRefAssetPartDTO>> getDetails(@RequestBody EamSparePartQuery query) {

        List<EamRefAssetPartDTO> list = eamSparePartService.getDetails(query);

        return RetResult.ok(list);
    }

}
